/*
 *  mini-SaaS
 *  Application for developing Software as a Service (SaaS) 
 *  Copyright (c) 2011 Anand Sudhakaran, Bryce Holton, Chao Zhang
 *  ASU
 *
 *  This file is part of mini-SaaS.
 *
 *  mini-SaaS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  mini-SaaS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
 *  along with mini-SaaS; if not, see <http://www.gnu.org/licenses/>. 
 *
 */

package loginbean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CalculateTotalService {
	
	Connection conn = null;
	Statement st=null;
	ResultSet rs= null;
	float result=0;

	
    public String calculateTotal(int tenantid)
    {
		try 
		{
			DBManager db = new DBManager();
			conn = db.getDatabaseConnection();

			String strQuery = "select value from datatable where tenantid = " + tenantid + " and GUID in (select (value - 1) as a from datatable where fieldID in (8,9,10,11) and value <> 0)"; //using the Cart Object to get the CartTotal by tenantid and customerid
			st = conn.createStatement();
			rs = st.executeQuery(strQuery);
			
			while (rs.next())
			{
				result += Float.valueOf(rs.getString("value").trim()).floatValue();
						
			}
			
			Statement st2;
			{
				String strQuery2 = "UPDATE datatable SET Value=" + result + " WHERE tenantid=" + tenantid + " and fieldid = 16";
				st2 = conn.createStatement();
				st2.execute(strQuery2);

			} 
			
			
			
			
			conn.close();
	
			
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		return "<form name='calculateTotalForm' method='post' action='Result2.jsp'> " +
				"The calculate total in cart is <br/>" +
				result +
				"<br/>" +
				"<input type='submit' name='Submit' value='Submit'>" +
				"<input type='hidden' name='CalculateTaxResult' value=" +
				result +
				"/>" +
				"</form>";
		
		

    }
}
